package com.ruibang.glass.quality.util;

import org.apache.poi.ss.usermodel.*;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-01-12  15:55
 * @Description: TODO
 * @Version: 1.0
 */
public class PoiUtils {

    public static void setGlobalStyle(Workbook workbook) {
        // 创建一个新样式
        CellStyle style = workbook.createCellStyle();
        // 设置样式属性，例如字体、颜色、边框等
        Font font = workbook.createFont();
        font.setFontName("Arial");
        font.setFontHeightInPoints((short) 10);
//        font.setBold(true);
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);

        // 将样式应用于整个工作簿
        for (Sheet sheet : workbook) {
            for (Row row : sheet) {
                for (Cell cell : row) {
                    cell.setCellStyle(style);
                }
            }
        }
    }


    public static String getValue(Sheet sheet, int rowIdx, int cellIdx) {
        Row row = sheet.getRow(rowIdx);
        Cell cell = row.getCell(cellIdx);
        if (cell.getCellType().equals(CellType.NUMERIC)) {
            // 如果是数值类型，则将数值转换为字符串
            return String.valueOf(cell.getNumericCellValue());
        } else if (cell.getCellType().equals(CellType.STRING)) {
            // 如果是字符串类型，则直接获取字符串值
            return cell.getStringCellValue();
        } else {
            return cell.getStringCellValue();
        }
    }


    public static boolean isEmptyCell(Cell cell) {
        if (cell == null || cell.getCellType().equals(CellType.BLANK)) {
            return true;
        }
        return false;
    }
}
